package com.example.evanzeng.viewpagertest;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.example.evanzeng.viewpagertest.ViewPager.MyTransformation;
import com.example.evanzeng.viewpagertest.adapter.CustomPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.blurry.Blurry;
import jp.wasabeef.glide.transformations.BlurTransformation;

public class MainActivityTest2 extends AppCompatActivity {

    private int pagerWidth;
    private List<Integer> imageViewList;
    private ViewPager viewPager;
    private CustomPagerAdapter pagerAdapter;
    private ImageView ivBlur;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.viewPager);
        ivBlur = findViewById(R.id.iv_blur);
        imageViewList = new ArrayList<>();

        imageViewList.add(R.mipmap.first);
        imageViewList.add(R.mipmap.second);
        imageViewList.add(R.mipmap.third);
        imageViewList.add(R.mipmap.fourth);
        imageViewList.add(R.mipmap.fifth);
        imageViewList.add(R.mipmap.s6);
        imageViewList.add(R.mipmap.s7);
        imageViewList.add(R.mipmap.s8);
        imageViewList.add(R.mipmap.s9);
        imageViewList.add(R.mipmap.s10);
        imageViewList.add(R.mipmap.s11);


        viewPager.setOffscreenPageLimit(2);
//        pagerWidth = (int) (getResources().getDisplayMetrics().widthPixels * 3.0f / 5.0f);
//        ViewGroup.LayoutParams lp = viewPager.getLayoutParams();
//        if (lp == null) {
//            lp = new ViewGroup.LayoutParams(pagerWidth, ViewGroup.LayoutParams.MATCH_PARENT);
//        } else {
//            lp.width = pagerWidth;
//        }
//        viewPager.setLayoutParams(lp);
//        viewPager.setPageMargin(-10);
        pagerAdapter = new CustomPagerAdapter(this, imageViewList);
        findViewById(R.id.activity_main).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return viewPager.dispatchTouchEvent(motionEvent);
            }
        });
        viewPager.setPageTransformer(true, new MyTransformation());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(imageViewList.size() / 2);
//        loadBlur(imageViewList.size() / 2);
        blurImage(imageViewList.size() / 2);
        pagerAdapter.setImageShow(new CustomPagerAdapter.ImageCallback() {
            @Override
            public void getImage(final Integer resouce, final ImageView ivShow) {
                //加载完成
                ivShow.setImageResource(resouce);
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
//                loadBlur(i);
                blurImage(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    /**
     * glide加载模糊度
     *
     * @param i
     */
    private void loadBlur(final int i) {
        //加载背景，
        Glide.with(MainActivityTest2.this)
                .load(imageViewList.get(i))
                .bitmapTransform(new BlurTransformation(MainActivityTest2.this, 25, 1))  // “23”：设置模糊度(在0.0到25.0之间)，默认”25";"4":图片缩放比例,默认“1”。
//                .centerCrop()
                .placeholder(R.mipmap.iv_default_pic).error(android.R.drawable.ic_menu_report_image).listener(new RequestListener<Integer, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, Integer model, Target<GlideDrawable> target, boolean isFirstResource) {
                Glide.with(MainActivityTest2.this)
                        .load(imageViewList.get(i))
//                        .centerCrop()
                        .bitmapTransform(new BlurTransformation(MainActivityTest2.this, 25, 1))  // “23”：设置模糊度(在0.0到25.0之间)，默认”25";"4":图片缩放比例,默认“1”。
                        .placeholder(R.mipmap.iv_default_pic).error(android.R.drawable.ic_menu_report_image).into(ivBlur);
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, Integer model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                return false;
            }
        }).into(ivBlur);
    }

    public void blurImage(int postion){
        Glide.with(this)
                .load(imageViewList.get(postion))
                .asBitmap()
                .centerCrop()
//                .override(150, 150)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
                        if (bitmap != null) {
                            Blurry.with(MainActivityTest2.this).
                                    radius(10). //值越大越模糊
                                    sampling(5). //对原图抽样
                                    async().from(bitmap).//对图片做高斯模糊
                                    into(ivBlur);
                        };
                    }
                });


    }

}
